#include "osObjects.h"  
#include "systimer.h"
#include "rtctime.h"
#include "wdog.h"
#include "ledctr.h"
#include "sprintf.h"
#include "pincfg.h"
#include "keycheck.h"
#include "adchandle.h"
#include "dataproc.h"
#include "at24xx.h"
#include "at25xx.h"
#include "candevice.h"
#include "mbdatacb.h"
#include "humitempctrlib.h"
#include "mbapp.h"

PID_Type TempPid;

uint32_t IdleTaskCnt=0;
/*----------------------------------------------------------------------------
 *      Global Functions
 *---------------------------------------------------------------------------*/
void PidInit(void);
void PidTest(void);
uint8_t bPidStart;
/*--------------------------- os_idle_demon ---------------------------------*/
   

/// \brief The idle demon is running when no other thread is ready to run
void os_idle_demon (void)
{
    uint8_t DispBuff[16];
    
    TimeCur.Year = 2016;
    TimeCur.Mon  = 12;
    TimeCur.Day  = 2;
    TimeCur.Hour = 12;
    TimeCur.Min  = 30;
    
    PrintPortInit(9600);
    KBIInit();
    AdcInit();
    PidInit();
    
    for (;;)
	{
        WDOG_Feed();
		SysPulseClear();
		SysPulseCreat();
        if(bPulse_1s){
            TimeRun();
            TimeToAscii(&TimeCur,DispBuff);
            sPrintf((char*)&DispBuff[0]);
            sPrintf(" \r\n\0");
        }
        IdleTaskCnt++;
        
        RegInput[0].uVal = TimeCur.Year;
        RegInput[1].uVal = TimeCur.Mon;
        RegInput[2].uVal = TimeCur.Day;
        RegInput[3].uVal = TimeCur.Hour;
        RegInput[4].uVal = TimeCur.Min;
        RegInput[5].uVal = TimeCur.Sec;
        
        LedCtrTask();
        KeyHandle();
        
        PidTest();
    }
}

void PidInit()
{
    uint16_t Par[5];
    
    Par[TEMP_PID_P_POS] = 50;
    Par[TEMP_PID_I_POS] = 240;
    Par[TEMP_PID_D_POS] = 120;
    Par[TEMP_PID_AR_POS] = 80;
    Par[TEMP_PID_T_POS] = 4;
    
    TempPid.Pv = 500;
    TempPid.Sv = 500;
    PidSet(&TempPid,Par,1000);
    PidClr(&TempPid);
    
    bPidStart = 1;
}

void PidTest()
{
    if(bPidStart)
        IndPid(&TempPid);
}

